/*
Copyright (C) 2010  Christoph Schnack, Berlin (Germany)

This file is part of Tribuit.

    Tribuit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Tribuit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tribuit.  If not, see <http://www.gnu.org/licenses/>.


########################################################################
*/
package Storage;
import java.util.HashMap;
import java.util.Vector;

/** 
* A Word is uniqued by it's name.

* @author Christoph Schnack
*
*/


public class Word{

	String termclassA,termclassB,section;
	Meaning mainA, mainB;
	Vector<Meaning> meaningsA,meaningsB;
	
	public Word(String sect,String tcA,String tcB,Vector<Meaning> mngsA,Vector<Meaning> mngsB){
		termclassA=tcA;
		termclassB=tcB;
		section=sect;
		//FIXME if argument==null ???
		meaningsA = mngsA;
		meaningsB = mngsB;
		mainA = meaningsA.elementAt(0);
		mainB = meaningsB.elementAt(0);
	}
	
	public void setAllArticlesA(String arg){
		for(Meaning m : meaningsA){
			m.setAllArticles(arg);
		}
	}
	public void setAllArticlesB(String arg){
		for(Meaning m : meaningsB){
			m.setAllArticles(arg);
		}
	}
	
	public String getTermclassA(){return termclassA;}
	public void setTermclassNameA(String arg){termclassA=arg;}
	public String getTermA(){return mainA.getMeaning();}
	public String getPhonA(){return mainA.getPhonetic();}
	public String getNoteA(){return mainA.getNote();}
	public String getPreFixA(){return mainA.getArticle();}
	public String getMetaA(String key){return mainA.getMetaElement(key);}
	public HashMap<String,String> getMetadescriptionA(){return mainA.getMetaDescription();}
	
	public void setTermclassNameB(String arg){termclassB=arg;}
	public String getTermclassB(){return termclassB;}
	public String getTermB(){return mainB.getMeaning();}
	public String getPhonB(){return mainB.getPhonetic();}
	public String getNoteB(){return mainB.getNote();}
	public String getPreFixB(){return mainB.getArticle();}
	public String getMetaB(String key){return mainB.getMetaElement(key);}
	public HashMap<String,String> getMetadescriptionB(){return mainB.getMetaDescription();}
	
	public String getName(){ return getTermA()+" - "+getTermB();}
	public String getNameInverse(){ return getTermB()+" - "+getTermA();}
	public String getLection(){return section;}
	public void setLection(String lec){section=lec;}
	/**
	 * Returns article only if available.
	 * @author Christoph Schnack
	 * @return (article) meaning 
	 */
	public String getFullTermA(){
		if(mainA.hasArticle())return "("+getPreFixA()+") "+getTermA();
		else return getTermA();
	}
	/**
	 * Returns article only if available.
	 * @author Christoph Schnack
	 * @return (article) meaning 
	 */
	public String getFullTermB(){
		if(mainB.hasArticle())return "("+getPreFixB()+") "+getTermB();
		else return getTermB();
	}
	
	/**
	 * Yields the String for saving.
	 * @return String for saving.
	 */
	public String getAllData(){
		StringBuffer mngASB=new StringBuffer(),mngBSB=new StringBuffer();
		for(Meaning m : meaningsA){
			mngASB.append((m.hasArticle()?m.getArticle()+"%":"~%")+m.getMeaning()+"%"+m.getCategory()+"+");
			if(m.hasSynonyms()){
				for(Term t : m.getSynonyms()){
					mngASB.append((t.hasArticle()?t.getArticle()+"%":"~%")+t.getTerm()+"%"+t.getCategory()+"+");
				}
			}
			else mngASB.append("~~~");
			mngASB.append(":"+(m.hasPhonetic()?m.getPhonetic()+":":"~:"));
			mngASB.append((m.hasNote()?m.getNote()+":":"~:"));
			mngASB.append((m.hasExampleSentence()?m.getExampleSentence()+":":"~:"));
			if(m.hasMeta()){
				for(String s:m.getMetaDescription().keySet().toArray(new String[m.getMetaDescription().keySet().size()])){
					String val = m.getMetaDescription().get(s);
					if(val.equals("")) val = "~";
					mngASB.append(s+"%"+val+"+");
				}
			}
			else mngASB.append("~~~");
			mngASB.append(";");
		}
		for(Meaning m : meaningsB){
			mngBSB.append((m.hasArticle()?m.getArticle()+"%":"~%")+m.getMeaning()+"%"+m.getCategory()+"+");
			if(m.hasSynonyms()){
				for(Term t : m.getSynonyms()){
					mngBSB.append((t.hasArticle()?t.getArticle()+"%":"~%")+t.getTerm()+"%"+m.getCategory()+"+");
				}
			}
			else mngBSB.append("~~~");
			mngBSB.append(":"+(m.hasPhonetic()?m.getPhonetic()+":":"~:"));
			mngBSB.append((m.hasNote()?m.getNote()+":":"~:"));
			mngBSB.append((m.hasExampleSentence()?m.hasExampleSentence()+":":"~:"));
			if(m.hasMeta()){
				for(String s:m.getMetaDescription().keySet().toArray(new String[m.getMetaDescription().keySet().size()])){
					String val = m.getMetaDescription().get(s);
					if(val.equals("")) val = "~";
					mngBSB.append(s+"%"+val+"+");
				}
			}
			else mngBSB.append("~~~");
			mngBSB.append(";");
		}
    	return section+">"+termclassA+">"+termclassB+">"+mngASB.toString()+">"+mngBSB.toString();
	}
	
	public void renameTermclassA(String nam){
		termclassA=nam;
	}
	public void renameTermclassB(String nam){
		termclassB=nam;
	}
	
	public void setTermclassA(String nam, HashMap<String,String> hm){
		termclassA = nam;
		for(Meaning m : meaningsA){
			m.setMeta(hm);
		}
	}
	
	public void setTermclassB(String nam, HashMap<String,String> hm){
		termclassB = nam;
		for(Meaning m : meaningsB){
			m.setMeta(hm);
		}
	}
	
	public boolean hasMeaningsA(){
		return (meaningsA.size()>1);
	}
	public boolean hasMeaningsB(){
		return (meaningsB.size()>1);
	}
	
	public void setMeaningsA(Vector<Meaning> arg){
		meaningsA=arg;
	}
	
	public void setMeaningsB(Vector<Meaning> arg){
		meaningsB=arg;
	}
	
	public Vector<Meaning> getMeaningVectorA(){
		return meaningsA;
	}
	
	public Vector<Meaning> getMeaningVectorB(){
		return meaningsB;
	}
	
	
	public void addMeaningA(Meaning mean1,String phonetic){
		mean1.setPhonetic(phonetic);
		meaningsA.add(mean1);
	}
	
	
	public void addMeaningB(Meaning mean1,String phonetic){
		mean1.setPhonetic(phonetic);
		meaningsB.add(mean1);
	}
	
	public void delMeaningA(String mean){
		for(int i=0;i<meaningsA.size();i++){
			if(meaningsA.elementAt(i).getMeaning().equals(mean)) meaningsA.remove(i);
		}
	}
	
	public void delMeaningB(String mean){
		for(int i=0;i<meaningsB.size();i++){
			if(meaningsB.elementAt(i).getMeaning().equals(mean)) meaningsB.remove(i);
		}
	}
	
	public void editMeaningA(String meaningOld,String artNew, String meaningNew){
		for(int i=0;i<meaningsA.size();i++){
			Meaning m = meaningsA.elementAt(i);
			if(m.getMeaning().equals(meaningOld)){
				m.setArticle(artNew);
				m.setMeaning(meaningNew);
				break;
			}
		}
	}
	
	public void editMeaningB(String meaningOld,String artNew, String meaningNew){
		for(int i=0;i<meaningsB.size();i++){
			Meaning m = meaningsB.elementAt(i);
			if(m.getMeaning().equals(meaningOld)){
				m.setArticle(artNew);
				m.setMeaning(meaningNew);
				break;
			}
		}
	}
	
	/**
	 * Generates a String with all meanings except the chief meaning.
	 * @return (art1) mng1 [cat1], (art2)... or an empty String
	 */
	public String getAllMeaningsA(){
		String returnString="";
		if(meaningsA.size()>0){
			StringBuffer sb = new StringBuffer();
			for(int i=0;i<meaningsA.size();i++){
				sb.append(meaningsA.elementAt(i).getMeaningAsStringFull());
				if(i<meaningsA.size()-1)sb.append(", ");
			}
			returnString=sb.toString();
		}
		return returnString;
	}
	
	/**
	 * Generates a String with all meanings except the chief meaning.
	 * @return (art1) mng1 [cat1], (art2)... or an empty String
	 */
	
	public String getAllMeaningsB(){
		if(meaningsB.size()>0){
			StringBuffer sb = new StringBuffer();
			for(int i=0;i<meaningsB.size();i++){
				sb.append(meaningsB.elementAt(i).getMeaningAsStringFull());
				if(i<meaningsB.size()-1)sb.append(", ");
			}
			return sb.toString();
		}
		return "";
	}
}